package com.tongji.housediy.service
{
	import com.tongji.housediy.events.brandEvent.GetAllBrandEvent;
	import com.tongji.housediy.events.brandEvent.GetGoodsByBrandEvent;
	import com.tongji.housediy.utils.LoadAdmin;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/11
	 */ 
	public class BrandService extends Actor
	{
		public var brandRemoteObject:RemoteObject;
		
		public function BrandService()
		{
			super();
					
			//var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediy/Gateway.aspx")];
			var amfChannel:Array = [new AMFChannel(null, "http://design.rryjw.com/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			brandRemoteObject = new RemoteObject("fluorine");
			brandRemoteObject.source = "HouseDiyService.HouseDiyAction";
			brandRemoteObject.showBusyCursor = true;
			brandRemoteObject.channelSet = channelSet;
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");
			brandRemoteObject.getOperation("getAllBrands").addEventListener(ResultEvent.RESULT, getAllBrandsResult);
			brandRemoteObject.getOperation("getAllBrands").addEventListener(FaultEvent.FAULT, faultHandler);
			
			brandRemoteObject.getOperation("getBrandXML").addEventListener(ResultEvent.RESULT, getBrandXMLResult);
			brandRemoteObject.getOperation("getBrandXML").addEventListener(FaultEvent.FAULT, faultHandler);
		
			brandRemoteObject.getOperation("getGoodsSortByBrand").addEventListener(ResultEvent.RESULT, getGoodsByBrandResult);
			brandRemoteObject.getOperation("getGoodsSortByBrand").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		

		public function getBrands():void
		{
			brandRemoteObject.getAllBrands();
		}
		
		public function getBrandXML():void
		{
			brandRemoteObject.getBrandXML();
		}
		
		public function getGoodsByBrand(brandID:int):void
		{
			brandRemoteObject.getGoodsSortByBrand(brandID);
		}
		
		private function getAllBrandsResult(event:ResultEvent):void
		{
			dispatch(new GetAllBrandEvent(GetAllBrandEvent.UPDATE_BRAND_LIST, null, event.result as ArrayCollection));
		}
		
		private function getBrandXMLResult(event:ResultEvent):void
		{
			dispatch(new GetAllBrandEvent(GetAllBrandEvent.UPDATE_BRAND_XML, event.result as XML, null));
		}
		
		private function getGoodsByBrandResult(event:ResultEvent):void
		{
			dispatch(new GetGoodsByBrandEvent(GetGoodsByBrandEvent.UPDATE_GOODS_BRAND, 0, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}